import string
import file_operations as file
from PorterStemmer import *


COMMONFILE = 'common.txt'
COMMONLIST = file.read(COMMONFILE).split()

def notCommon(string):
    """
    Return true if string is not a common word
    """
    return not string in COMMONLIST 

def notPunct(char):
    """
    Return true if char is not a punctuation, false if it
    is a punctuation. 
    """
    return not char in string.punctuation

def removePunct(string):
    """
    Return a string with all the punctuation removed
    """
    return filter(notPunct, string)

def getStem(string):
    """
    Calls the PorterStemmer class to stem the string
    
    Return a stemmed version of the string
    """
    p = PorterStemmer()
    return PorterStemmer.stem(p, string, 0, len(string)-1)          

def parseString(input_string):
    """
    Main method in this module. 
    
    First format the string using formatString method, 
    then stem each word using PorterStemmer, 
    and append it to a new list. 
    
    Return a list of stemmed words. 
    """
    word_list = formatString(input_string)
    stem_list = makeStemList(word_list) 
    return stem_list

def makeStemList(word_list):
    stem_list = []
    for string in word_list:
        stem_list.append(getStem(string))
    return stem_list

def formatString(input_string):
    """
    Does several operations to a string, 1) remove
    punctuation, 2)convert to lower case, and then 3) split it 
    according to white spaces, 4) remove common words 
    and finally 5)return a list of the result
    """
    input_string = removePunct(input_string).lower()
    return removeUnwanted(input_string.split())

def removeUnwanted(word_list):   
    return filter(notCommon, word_list)

if __name__ == '__main__':
    print formatString("mating Partners.")
